package kindatamanip;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
/**
 *
 * @author thbrandston
 */
public class GenerateData
{

    // creates a genome for the whole population to draw from
    // variation determines the number of different alleles at each locus,
    //   could be made int an int[] for greater control
    public static ArrayList<ArrayList<Integer>> generatePopulationGenome(int nloci, int variation)
    {
        ArrayList<ArrayList<Integer>> loci = new ArrayList(0);
        for(int i = 0; i < nloci; i++)
        {
            ArrayList<Integer> tmpList = new ArrayList(0);
            for(int j = 0; j < variation; j++)
                tmpList.add(j);
            loci.add(tmpList);
        }
        return loci;
    }

    //fills the passed (empty) arrays with generated data
    public static void generatePandC(DipOrg[] par, DipOrg[] chil, ArrayList<ArrayList<Integer>> loc)
    {
        //for each parent
        for(int i = 0; i < par.length; i++)
        {
            par[i] = new DipOrg(loc.size(), loc.get(0).size());

            //stick two random alleles for each locus into the parent at that locus
            for(int j = 0; j < loc.size(); j++)
            {
                int ls = loc.get(j).size();
                par[i].setLocus(j, loc.get(j).get((int)Math.floor(Math.random()*ls)),
                                   loc.get(j).get((int)Math.floor(Math.random()*ls)));
            }
        }

        //for each child
        for(int i = 0; i < chil.length; i++)
        {
            chil[i] = new DipOrg(loc.size(), loc.get(0).size());
            
            //pick two parents, take a random allele from each for each locus
            DipOrg p1, p2;
            p1 = par[(int)Math.floor(Math.random()*par.length)];
            do p2 = par[(int)Math.floor(Math.random()*par.length)];
            while(p1 == p2);

            p1.addChild(chil[i]);
            p2.addChild(chil[i]);
            chil[i].setParents(p1, p2);
            
            for(int j = 0; j < loc.size(); j++)
            {
                chil[i].setLocus(j, p1.getEitherAlleleAt(j),
                                    p2.getEitherAlleleAt(j));
            }
        }

    }

    // write the data to the format used by WPFGraph
    public static void writeForWPFG(String filename, DipOrg[] par, DipOrg[] chil, String linkMethod)
    {
        File f = new File(filename + ".xml");
        DataOutputStream out;
        
        if(f.exists())
            f.delete();
        
        try
        {
            f.createNewFile();
            out = new DataOutputStream(new FileOutputStream(f));
            
            out.writeBytes("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n");
            out.writeBytes("<graph version=\"1.0\">\n");
            out.writeBytes("  <nodes>\n");
            for(int i = 0; i < par.length; i++)
            {
                out.writeBytes("    <node id=\""+ i +"\" color=\"#FFFFCCCC\" marked=\"False\" "
                        + "position=\"" + (i*20 - (par.length*20)/2 ) + ",200,50\" text=\"A0\" />\n");
            }
            for(int i = 0; i < chil.length; i++)
            {
                out.writeBytes("    <node id=\""+ (i+par.length) +"\" color=\"#F0FFFFFF\" marked=\"False\" "
                        + "position=\"" + ((i%par.length)*20 - (par.length*40)/2 )
                        + "," + ((i/par.length)*20 - 100) + ",0\" text=\"A0\" />\n");
            }
            out.writeBytes("  </nodes>\n");
            out.writeBytes("  <edges>\n");
            
            // link parents and children
            for(int i = 0; i < chil.length; i++)
            {
                for(int j = 0; j < par.length; j++)
                {
                    if(chil[i].hasParent(par[j]))
                    {
                        out.writeBytes("<edge firstnode=\"" + (i + par.length)
                                + "\" secondnode=\"" + j + "\" color=\"#20808080\""
                                + " direction=\"OmniDirectional\" marked=\"False\" weight=\"-1\" />\n");
                    }
                }
                
            }

            // link children to each other
            for(int i = 0; i < chil.length; i++)
            {
                for(int j = i; j < chil.length; j++)
                {
                    //int w = chil[i].compareAlleles(chil[j]);
                    //System.out.println(w);
//                    if(w > 0)
//                    {
//                        out.writeBytes("<edge firstnode=\"" + (i + par.length)
//                                + "\" secondnode=\"" + (j + par.length) + "\" color=\"#20");
//
//                        //if(w < )
//
//                        out.writeBytes("\" direction=\"OmniDirectional\" marked=\"False\" "
//                                + "weight=\"" + w + "\" />\n");
//                    }
                }
            }
            
//    "<edge firstnode=\"0\" secondnode=\"7\" color=\"#FFD3D3D3\" direction=\"OmniDirectional\" marked=\"False\" weight=\"1\" />"
            out.writeBytes("  </edges>\n");
            out.writeBytes("</graph>\n");
        }
        catch(Exception e)
        {
            System.out.println(e);
            return;
        }

    }

}